package com.ctg.behavior.calc.impala.transfer.tracks;

import java.util.Date;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLConsts;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtSQLTransfer;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.tracks.CtTracksBody;
import com.ctg.behavior.utils.CtDateUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtTracksTransfer implements CtSQLTransfer {

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtTracksBody)) {
			log.warn("calcObject is not CtTracksBody, class={}", calcObject.getClass());
			return "";
		}
		CtTracksBody calcBody = (CtTracksBody) calcObject;
		log.info("CtTracksBody={}", JSON.toJSONString(calcBody));

		// 组装SQL
		StringBuilder impalaSQL = new StringBuilder();

		impalaSQL.append(" SELECT event AS event, track_id AS trackId, user_id AS userId, bd_scence_id AS bdScenceId ");
		impalaSQL.append(" , event_time AS eventTime ");
		for(String oneItem : calcBody.getProfiles()) {
			impalaSQL.append(" , ").append(oneItem).append(" AS ").append(oneItem);
		}
		
		if("TRUE".equalsIgnoreCase(sqlParam.getParam(CtSQLConsts.KEY_IS_RT_DATA))){
			impalaSQL.append(" FROM a_event_kudu_").append(calcBody.getAppId());
		} else {
			impalaSQL.append(" FROM a_event_parquet_").append(calcBody.getAppId());
		}
		
		impalaSQL.append(" WHERE 1=1 ");
		
		//时间范围限制
		{
			//时间范围限制 - 起始时间
			Date startDate = CtDateUtils.stringToDate(calcBody.getStartTime(), CtDateUtils.YYYY_MM_DD_HH_MM_SS);
			impalaSQL.append(" AND event_time >= '").append(startDate.getTime()).append("' ");
			//时间范围限制 - 结束时间
			if(!StringUtils.isEmpty(calcBody.getEndTime())) {
				Date endDate = CtDateUtils.stringToDate(calcBody.getEndTime(), CtDateUtils.YYYY_MM_DD_HH_MM_SS);
				impalaSQL.append(" AND event_time <= '").append(endDate.getTime()).append("' ");
			}
		}
		//事件限制
		if(!StringUtils.isEmpty(calcBody.getEventId())) {
			impalaSQL.append(" AND event = '").append(calcBody.getEventId()).append("' ");
		}
		//trackId限制
		if(!StringUtils.isEmpty(calcBody.getTrackId())) {
			impalaSQL.append(" AND track_id = '").append(calcBody.getTrackId()).append("' ");
		}
		//URL ： 模糊匹配
		if(!StringUtils.isEmpty(calcBody.getUrl())) {
			impalaSQL.append(" AND _url like '%").append(calcBody.getUrl()).append("%' ");
		}
		//扩展条件信息
		Set<Entry<String, String>> entries = calcBody.getExtInfo().entrySet();
		for(Entry<String, String> entry : entries) {
			impalaSQL.append(" AND ").append(entry.getKey()).append(" = '").append(entry.getValue()).append("' ");
		}
		//ORDER BY - event_time
		impalaSQL.append(" ORDER BY event_time DESC ");
		
		//分页限制
		if (calcBody.getLimit() >= 0) {
			Integer currentPage = calcBody.getCurrentPage();
			currentPage = currentPage - 1;
			currentPage = (currentPage > 0 ? currentPage : 0);
			Integer startPos = currentPage * calcBody.getPageSize();
			Integer endPos = (1 + currentPage) * calcBody.getPageSize();
			if (endPos > calcBody.getLimit() && calcBody.getLimit() != 0) {
				endPos = calcBody.getLimit();
			}
			Integer limitSize = endPos - startPos;
			limitSize = limitSize > 0 ? limitSize : 0;
			impalaSQL.append(" LIMIT ").append(limitSize).append(" OFFSET ").append(startPos);
		}

		return impalaSQL.toString();
	}

}
